﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;
using FU.Capstones.IMS.Services.Common.Respondents;
using FU.Capstones.IMS.Services.Interfaces;

namespace FU.Capstones.IMS.Services.DAOs
{
    public class EmploymentTypeDAO : DAOBase, IEmploymentTypeDAO
    {
        #region IEmploymentTypeDAO Members

        public CollectionFeedback<EmploymentType> GetCollection(bool isShowAll)
        {
            var feedback = new CollectionFeedback<EmploymentType>
                               {IsSuccessful = false, Data = new List<EmploymentType>()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionEmploymentType", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var employmentType = new EmploymentType();
                    employmentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
                    employmentType.EmploymentTypeCode = Convert.ToString(reader["EmploymentTypeCode"]);
                    employmentType.EmploymentTypeName = Convert.ToString(reader["EmploymentType"]);
                    employmentType.BriefDescription = Convert.ToString(reader["BriefDescription"]);
                    employmentType.FullDescription = Convert.ToString(reader["FullDescription"]);
                    employmentType.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                    feedback.Data.Add(employmentType);

                    //break;
                }
                feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                feedback.IsSuccessful = true;

                //Log.Info("GetCollectionEmploymentType: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                //Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            catch (Exception t)
            {
                //Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<EmploymentType> GetCollectionType(bool isShowAll)
        {
            var feedback = new CollectionFeedback<EmploymentType>
                               {IsSuccessful = false, Data = new List<EmploymentType>()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionEmploymentType", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var employmentType = new EmploymentType();
                    employmentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
                    employmentType.EmploymentTypeCode = Convert.ToString(reader["EmploymentTypeCode"]);
                    employmentType.EmploymentTypeName = Convert.ToString(reader["EmploymentType"]);
                    employmentType.BriefDescription = Convert.ToString(reader["BriefDescription"]);
                    employmentType.FullDescription = Convert.ToString(reader["FullDescription"]);
                    employmentType.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                    feedback.Data.Add(employmentType);

                    //break;
                }
                feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                feedback.IsSuccessful = true;

                //Log.Info("GetCollectionEmploymentType: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                //Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            catch (Exception t)
            {
                //Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<EmploymentType> GetSingle(int objectID)
        {
            var feedback = new SingleFeedback<EmploymentType> {IsSuccessful = false, Data = new EmploymentType()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("GetSingleEmploymentType", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@EmploymentTypeID", objectID);

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var employmentType = new EmploymentType();
                        employmentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
                        employmentType.EmploymentTypeCode = Convert.ToString(reader["EmploymentTypeCode"]);
                        employmentType.EmploymentTypeName = Convert.ToString(reader["EmploymentType"]);
                        employmentType.BriefDescription = Convert.ToString(reader["BriefDescription"]);
                        employmentType.FullDescription = Convert.ToString(reader["FullDescription"]);
                        employmentType.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                        feedback.Data = employmentType;

                        break;
                    }
                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.GetFailed;
                }

                //Log.Info("GetSingleEmploymentType: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            catch (Exception t)
            {
                //Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<int> Insert(EmploymentType data)
        {
            var feedback = new SingleFeedback<int> {IsSuccessful = false, Data = 0};
            try
            {
                OpenConnection();

                var command = new SqlCommand("InsertEmploymentType", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@EmploymentTypeCode", data.EmploymentTypeCode);
                command.Parameters.AddWithValue("@EmploymentType", data.EmploymentTypeName);
                command.Parameters.AddWithValue("@BriefDescription", data.BriefDescription);
                command.Parameters.AddWithValue("@FullDescription", data.FullDescription);

                feedback.Data = command.ExecuteNonQuery();

                if (feedback.Data >= 1)
                {
                    feedback.Message = InventoryConstants.Messages.InsertSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.InsertFailed;
                }

                Log.Info("InsertEmploymentType: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.InsertFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.InsertFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Update(EmploymentType data)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                OpenConnection();

                var command = new SqlCommand("UpdateEmploymentType", imsConnection);

                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@EmploymentTypeID", data.EmploymentTypeID);
                command.Parameters.AddWithValue("@EmploymentTypeCode", data.EmploymentTypeCode);
                command.Parameters.AddWithValue("@EmploymentType", data.EmploymentTypeName);
                command.Parameters.AddWithValue("@BriefDescription", data.BriefDescription);
                command.Parameters.AddWithValue("@FullDescription", data.FullDescription);

                int rowAffect = command.ExecuteNonQuery();

                if (rowAffect < 1)
                {
                    feedback.Message = InventoryConstants.Messages.UpdateFailed;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }

                Log.Info("UpdateEmploymentType: {0}, {1}, {2}", data.EmploymentTypeID, feedback.Message,
                         DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.UpdateFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.UpdateFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Delete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};

            try
            {
                OpenConnection();

                var command = new SqlCommand("DeleteEmploymentType", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@EmploymentTypeID", objID);

                int rowAffect = command.ExecuteNonQuery();

                if (rowAffect >= 1)
                {
                    feedback.Message = InventoryConstants.Messages.DeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.DeleteFailed;
                }

                Log.Info("DeleteEmploymentType: {0}, {1}, {2}", objID, feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.DeleteFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.DeleteFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<EmploymentType> Search(EmploymentType criteria)
        {
            var feedback = new CollectionFeedback<EmploymentType>
                               {IsSuccessful = false, Data = new List<EmploymentType>()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("SearchEmploymentType", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@EmploymentType",
                                                String.IsNullOrEmpty(criteria.EmploymentTypeName)
                                                    ? criteria.EmploymentTypeName
                                                    : criteria.EmploymentTypeName.Trim());

                SqlDataReader reader = command.ExecuteReader();

                var employmentTypes = new List<EmploymentType>();

                while (reader.Read())
                {
                    var employmentType = new EmploymentType();
                    employmentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
                    employmentType.EmploymentTypeCode = Convert.ToString(reader["EmploymentTypeCode"]);
                    employmentType.EmploymentTypeName = Convert.ToString(reader["EmploymentType"]);
                    employmentType.BriefDescription = Convert.ToString(reader["BriefDescription"]);
                    employmentType.FullDescription = Convert.ToString(reader["FullDescription"]);
                    employmentType.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                    employmentTypes.Add(employmentType);

                    //break;
                }
                feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                feedback.IsSuccessful = true;
                feedback.Data = employmentTypes;

                //Log.Info("GetCollectionEmploymentType: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                //Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            catch (Exception t)
            {
                //Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.GetFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback UndoDelete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};

            try
            {
                OpenConnection();

                var command = new SqlCommand("UndoDeletedEmploymentType", imsConnection);

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@EmploymentTypeID", objID);

                int rowAffect = command.ExecuteNonQuery();

                if (rowAffect >= 1)
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteFailed;
                }
                Log.Info("UndoDeletedEmploymentType: {0}, {1}, {2}", objID, feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.UndoDeleteFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.UndoDeleteFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        #endregion
    }
}